/**
 * nghilt
 * 2011/09/16
 *
 * Copyright by N&N.,Inc
 */
package vn.nnn.shogi.quanco;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;

import javax.swing.JApplet;

import at.com.vn.chess.util.RectangleUtils;

import vn.nnn.shogi.board.Board;

/**
 * 
 */
public class QuanXe extends QuanCo {

	public QuanXe( Rectangle toaDo, int color, boolean normalDirection, JApplet mainApplet ) {
		super( toaDo, color, normalDirection, mainApplet );
	}
	
	@Override
	public void setToaDoNhaTu() {
		toaDoNhaTu = new Point( 0, 0 );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canMove(java.awt.Rectangle, vn.nghi.test6.QuanCo, java.util.ArrayList)
	 */
	@Override
	public boolean canMove( Board board, Rectangle destination,
			QuanCo foundedPiece, ArrayList<QuanCo> listQuanCo ) {
		
		// Neu dung nguyen tai cho thi khong tinh la mot luot di chuyen
		if( toaDo == destination ) {
			return false;
		}
		
		// Neu khong cung mot hang hoac cung mot cot
		if( !( RectangleUtils.isInSameColumn( toaDo, destination ) 
				|| RectangleUtils.isInSameRow( toaDo, destination ) ) ) {
			return false;
		}
		
		// Neu bi can
		if ( biCan( destination, listQuanCo ) ) {
			return false;
		}
		
		return isPassToBasicMoveCondition( board, destination, foundedPiece, listQuanCo );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#upgrade(java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public void upgrade( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {		
		QuanXePhongCap xePhongCap = new QuanXePhongCap( destination, color, normalDirection, getMainApplet() );
		int idxChosen = getPositionInList( listQuanCo );
		
		listQuanCo.remove( idxChosen );
		listQuanCo.add( idxChosen, xePhongCap );
	}

	/* (non-Javadoc)
	 * @see vn.nnn.shogi.quanco.QuanCo#canRelease(vn.nnn.shogi.board.Board, java.awt.Rectangle, java.util.ArrayList)
	 */
	@Override
	public boolean canRelease( Board board, Rectangle destination,
			ArrayList<QuanCo> listQuanCo ) {		
		return isPassToBasicReleaseCondition( board, destination, listQuanCo );
	}
	
	public boolean biCan( Rectangle destination, ArrayList<QuanCo> listQuanCo ) {
		
		int row = RectangleUtils.convertToRealBoardRow( toaDo );
		int column = RectangleUtils.convertToRealBoardCol( toaDo );
		int nextRow = RectangleUtils.convertToRealBoardRow( destination );
		int nextCol = RectangleUtils.convertToRealBoardCol( destination );
		
		int max;
		int min;

		// Neu cung hang thi kiem tra giua cac cot xem co con nao khong
		if ( row == nextRow ) {
			max = Math.max( column, nextCol );
			min = Math.min( column, nextCol );
			
			for ( int i = 0; i < listQuanCo.size(); i++ ) {				
				if( RectangleUtils.isInSameRow( toaDo, listQuanCo.get( i ).toaDo ) 
						&& ( RectangleUtils.convertToRealBoardCol( listQuanCo.get( i ).toaDo ) > min 
								&& RectangleUtils.convertToRealBoardCol( listQuanCo.get( i ).toaDo ) < max ) ) {
					return true;
				}
			}
		}
		// Neu cung cot thi kiem tra giua cac hang xem co con nao khong
		else if ( column == nextCol ) {
			max = Math.max( row, nextRow );
			min = Math.min( row, nextRow );
			
			for ( int j = 0; j < listQuanCo.size(); j++ ) {
				if( RectangleUtils.isInSameColumn( toaDo, listQuanCo.get( j ).toaDo ) 
						&& ( RectangleUtils.convertToRealBoardRow( listQuanCo.get( j ).toaDo ) > min 
								&& RectangleUtils.convertToRealBoardRow( listQuanCo.get( j ).toaDo ) < max ) ) {
					return true;
				}
			}
		}

		return false;
	}	
}
